<?php

abstract class Dcmp_Component_View extends Kd_Object {

    private $_template = NULL;
    private $_templatePath = NULL;
    private $_component = NULL;

    public function __construct($component){
    	$this->_component = $component;
    	$this->initialize();
    }
    
    protected function initialize(){}

    protected function __($langKey){
        return Kd_Localize_Helper::__($langKey);
    }

    protected function __f(){
        $args = func_get_args();
        return call_user_func_array(array('Kd_Localize_Helper', '__f'), $args);
    }
    
    protected function getComponent(){
    	return $this->_component;
    }
    
    protected function getDomain(){
    	return $this->getComponent()->getDomain();
    }

    protected function getRequest(){
        return Kd_Web::getRequest();
    }

    protected function getResponse(){
        return Kd_Web::getResponse();
    }

    protected function getSession(){
        return Kd_Web::getSession();
    }

    protected function getVariables(){
        return Kd_Web::getVariables();
    }

    protected function getNavigation(){
        return Kd_Web::getRouter();
    }

    protected function getMessager(){
        return Kd_Web::getMessager();
    }

    protected function getRouter(){
        return Kd_Web::getRouter();
    }

    protected function getPage(){
        return Kd_Web::getPage();
    }

    protected function getIdentity(){
        return Kd_Web::getIdentity();
    }

    protected function getUrl($arguments = array(), $action = '*', $controller = '*', $component = '*'){
        return $this->getRouter()->buildUrl("$component/$controller/$action", $arguments);
    }

    protected function getHistoryUrl($actionPath){
    	$actionPath = $this->getRouter()->buildUrl($actionPath, NULL);
        return Kd_Web::getHistory()->get($actionPath);
    }

    public function getTemplatePath() {
        if (is_null($this->_templatePath)) {
            $path = str_replace('/', DIRECTORY_SEPARATOR, Kd_Web::getRouter()->getActionPath());
            $path = str_replace('-', DIRECTORY_SEPARATOR, $path);
            $path = str_replace('_', DIRECTORY_SEPARATOR, $path);
            $path = 'component'.DIRECTORY_SEPARATOR.$path;
            $this->_templatePath = Kd_Util_IO::findFile($this->getDomain()->getConfig()->getSetting('templateDirectories'), $path.'.phtml');
        }
        return $this->_templatePath;
    }
    
    public function setTemplatePath($value) {
        if(file_exists($value)){
            $this->_templatePath = $value;
            return $this;
        }
        
        $arr = explode('/', Kd_Web::getRouter()->getActionPath());
        if (!is_null($value)) {
            $values = explode('/', $value);
            $count = count($values);
            if ($count > 2) {
                $arr[0] = $values[0];
                $arr[1] = $values[1];
                $arr[2] = $values[2];
            } elseif ($count > 1) {
                $arr[1] = $values[1];
                $arr[2] = $values[2];
            } elseif ($count > 0) {
                $arr[2] = $values[0];
            }
        }

        $path = 'component'.DIRECTORY_SEPARATOR.str_replace('-', DIRECTORY_SEPARATOR, str_replace('_', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $arr)));
        $this->_templatePath = Kd_Util_IO::findFile(
        						$this->getDomain()->getConfig()->getSetting('templateDirectories'), 
        						$path.'.phtml');
        return $this;
    }

    public function getTemplate(){
        if(is_null($this->_template)){
            $this->_template = new Dcmp_Template($this->getDomain());
        }
        return $this->_template;
    }

    public function render() {
        return $this->getTemplate()
        				->assign('domain', $this->getDomain())
        				->render($this->getTemplatePath());
    }
}